package uni.call.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class LogActivity extends Activity  
{
	LogDBAdapter myAdapter; 
	Cursor cursor;
	String[] columns;
	int[] to;
	SimpleCursorAdapter mAdapter;
    public void onCreate(Bundle savedInstanceState) 
    {
        //
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.log);

        Button bt = (Button)findViewById(R.id.clearlog);
        bt.setOnClickListener(ClearListener);
        
        myAdapter = new LogDBAdapter(this);
    }
    @Override
    public void onStart()
    {
    	super.onStart();
        FillList();
    }

    public void FillList()
    {
    	ListView ListLog = (ListView)findViewById(R.id.listLog);
        myAdapter.OpenToRead();

        Cursor cursor = myAdapter.GetAll();
        startManagingCursor(cursor);

        // the desired columns to be bound
        String[] columns = new String[] { LogDBAdapter.KEY_Name, LogDBAdapter.KEY_Type};
        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.LogName, R.id.LogType};

        // create the adapter using the cursor pointing to the desired data as well as the layout information
        final SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.logrow, cursor, columns, to);

        // set this adapter as your ListActivity's adapter
        ListLog.setAdapter(mAdapter);
        
        myAdapter.Close();

        ListLog.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int i, long l) {
            	Cursor LogCursor = (Cursor) mAdapter.getItem(i);
        		startManagingCursor(LogCursor);
        		
        		String Type = LogCursor.getString(1);
        		final String Name = LogCursor.getString(2);
        		final String Phone = LogCursor.getString(3);
        		String Duration = LogCursor.getString(4);
        		String Time = LogCursor.getString(5);
        		
        						
        		final String[] Options = new String[1];
            	Options[0] = 
            		getString(R.string.Mobile) +": "+ Phone
            		+"\n"+
            		getString(R.string.Duration) +": "+ Duration
            		+"\n"+
            		Time;
            	
            	AlertDialog.Builder alertDialog  = new AlertDialog.Builder(v.getContext());
            	alertDialog.setTitle(Type+"\t\t"+Name);
            	alertDialog.setItems(Options, new DialogInterface.OnClickListener() 
            	{
				    public void onClick(DialogInterface dialog, int item) 
				    {
				    	ConnectivityManager manager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		         	    Boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
		         	    
		         	    if(isWifi)
		         	    {
			            	if(HomeActivity.Registered.equals("true"))
			            	{
			            		DailerActivity.Peer = Phone;
			            		OutGoingCall.PeerProfileName = Name;
			            		
				            	Intent OutgoingIntent = new Intent(getApplicationContext(), OutGoingCall.class);
				            	OutgoingIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
				            	OutgoingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				            	startActivity(OutgoingIntent);
			            	}
			            	else
			            	{
								AlertDialog.Builder builder2 = new AlertDialog.Builder(getApplicationContext());
								builder2.setTitle(getString(R.string.NotRegisteredTitle));
								builder2.setMessage(getString(R.string.NotRegisteredMessage));
								builder2.setNeutralButton("Ok", null);
								builder2.show();
			            	}
		         	    }
		         	    else
		         	    {
							AlertDialog.Builder builder2 = new AlertDialog.Builder(getApplicationContext());
							builder2.setTitle(getString(R.string.NoWiFiTitle));
							builder2.setMessage(getString(R.string.NoWiFiMessage));
							builder2.setNeutralButton("Ok", null);
							builder2.show();
		         	    }
				    }
            	});
            	alertDialog.show();
            }
        });
    }

    private OnClickListener ClearListener = new OnClickListener()
    {
        public void onClick(View v) 
        {
            myAdapter.OpenToWrite();
            myAdapter.DeleteAll();
            myAdapter.Close();
            FillList();
        }
    };
}
